﻿using Newtonsoft.Json;
using System.Data;

namespace OnWork.DataCenter.BLL.Standard.Conver
{
    /// <summary>
    /// 闫家沟代码转换
    /// 2025年6月24日15:00:22
    /// </summary>
    public class JsonToDataTableYJGConver : IJsonToDataTableConver
    {
        public DataTable Conver(string jsonStr, JsonToDataTableConfig config)
        {
            YJGMQTTModel? results = JsonConvert.DeserializeObject<YJGMQTTModel>(jsonStr);
            if (results != null)
            {
                DataTable dataTable = new DataTable();
                //创建列
                dataTable.Columns.Add(new DataColumn("SensorID", typeof(string))); //传感器ID
                dataTable.Columns.Add(new DataColumn("SensorLocation", typeof(string))); //传感器描述
                dataTable.Columns.Add(new DataColumn("SensorValue", typeof(string))); //传感器的值
                dataTable.Columns.Add(new DataColumn("CreateTime", typeof(string))); //创建时间
                //dataTable.Columns.Add(new DataColumn("DataTime", typeof(DateTime))); //数据时间

                string datetimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                //判断消息名称是否为空
                if (results.messageName.IsNotEmptyOnWork())
                {
                    //创建行
                    DataRow dr = dataTable.NewRow();
                    dr[0] = $"type{results.equipmentType}_{results.messageName}";// item.id;                //SensorID
                    dr[1] = $"type{results.equipmentType}_{results.messageName}";          //SensorLocation
                    dr[2] = results.messageValue;
                    dr[3] = datetimeStr;
                    dataTable.Rows.Add(dr);
                }

                //添加子数据判断
                ADDData(results.sMDate, results.equipmentType, dataTable, datetimeStr);
                ADDData(results.tMDate, results.equipmentType, dataTable, datetimeStr);
                ADDData(results.lSDate, results.equipmentType, dataTable, datetimeStr);

                return dataTable;
            }

            return null;
        }

        private void ADDData(Dictionary<string, string>? keyValues, int equipmentType, DataTable dataTable,string datetimeStr)
        {
            if (keyValues == null)
            {
                return;
            }

            foreach (var item in keyValues)
            {
                //创建行
                DataRow dr = dataTable.NewRow();

                dr[0] = $"type{equipmentType}_{item.Key}";// item.id;                //SensorID
                dr[1] = $"type{equipmentType}_{item.Key}";          //SensorLocation
                dr[2] = item.Value;
                dr[3] = datetimeStr;
                dataTable.Rows.Add(dr);
            }
        }
    }

    public class YJGMQTTModel
    {
        /// <summary>
        /// 设备类型
        /// </summary>
        public int equipmentType { get; set; }

        /// <summary>
        /// 消息名称
        /// </summary>
        public string? messageName { get; set; }

        /// <summary>
        /// 消息数值
        /// </summary>
        public float messageValue { get; set; }

        /// <summary>
        /// 时间
        /// </summary>
        public Dictionary<string, string>? tMDate { get; set; }

        /// <summary>
        ///返回子数据
        /// </summary>
        public Dictionary<string, string>? sMDate { get; set; }

        /// <summary>
        /// 时间
        /// </summary>
        public Dictionary<string, string>? lSDate { get; set; }
    }
}